package fun.flyee.android.pengshan.utils;

import android.os.CountDownTimer;

public class AppTimer extends CountDownTimer {

    private int count = 0;

    private OnTickListener onTickListener;
    private OnFinishListener onFinishListener;

    public AppTimer(long millisInFuture,
                    long countDownInterval,
                    OnTickListener onTickListener,
                    OnFinishListener onFinishListener) {
        super(millisInFuture, countDownInterval);
        this.onTickListener = onTickListener;
        this.onFinishListener = onFinishListener;
    }

    @Override
    public void onTick(long millisUntilFinished) {
        count++;
        if (onTickListener != null) {
            onTickListener.onTick(count);
        }
    }

    @Override
    public void onFinish() {
        if (onFinishListener != null) {
            onFinishListener.onFinish();
        }
    }

    public interface OnFinishListener {
        void onFinish();
    }

    public interface OnTickListener {
        void onTick(int count);
    }

    public static void begin(long timeout, long interval, OnFinishListener onFinishListener) {
        begin(timeout, interval, null, onFinishListener);
    }

    public static void begin(long timeout, long interval, OnTickListener onTickListener, OnFinishListener onFinishListener) {
        new AppTimer(timeout, interval, onTickListener, onFinishListener).start();
    }
}
